package api

import (
	"fmt"
	"gitee.com/alex_li/beacon-config/codec/mingkong"
	"gitee.com/alex_li/beacon-config/codec/wotian"
	"github.com/kataras/iris/v12"
	"github.com/lishimeng/app-starter"
)

type Req struct {
	Seconds int `json:"seconds,omitempty"` // 最大10000
}

type Resp struct {
	app.Response
	FPort   int    `json:"fPort,omitempty"`
	Payload string `json:"payload,omitempty"`
}

func codec(ctx iris.Context) {

	var err error
	var req Req
	var resp Resp
	var category = ctx.Params().Get("type")
	err = ctx.ReadJSON(&req)
	if err != nil {
		resp.Code = 500
		resp.Message = err.Error()
		_ = ctx.JSON(resp)
		return
	}

	if category == "mingkong" {
		resp.Payload, resp.FPort = mingkong.Encode(req.Seconds)
	} else if category == "wotian" {
		resp.Payload, resp.FPort = wotian.Encode(uint16(req.Seconds))
	} else {
		err = fmt.Errorf("unknonwn category")
	}

	if err != nil {
		resp.Code = 500
		_ = ctx.JSON(resp)
		return
	}
	resp.Code = 200
	_ = ctx.JSON(resp)
}
